package com.bieyang.anjia.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;
import android.widget.Scroller;

public abstract class BaseSlideActivity extends Activity {
	private View rootView = null;
	private View downView = null;
	private Scroller mScroller;
	private int width = 0;
	private int height = 0;

	private float SLIDE_POINT = 100.0F;
	private int DURATION = 1000;
	private int UP_STATE = -1;
	private boolean MOVE_STATE = false;
	private static final int STATE_OVER = 1;
	private static final int STATE_NOT_OVER = 0;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.rootView = setView();
		initView();
		initObject();
		initListener();
		initData();
		init();
	}

	private View setView() {
		BGView bgView = new BGView(this);

		this.downView = new View(this);
		this.downView.setBackgroundColor(-16777216);
		bgView.addView(this.downView);

		View view = getLayoutInflater().inflate(getLayoutID(), null);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				-1, -1);
		view.setLayoutParams(params);

		bgView.addView(view);
		setContentView(bgView);
		return view;
	}

	public abstract int getLayoutID();

	public abstract void initData();

	public abstract void initObject();

	public abstract void initListener();

	public abstract void initView();

	private void init() {
		DisplayMetrics dm = new DisplayMetrics();

		getWindowManager().getDefaultDisplay().getMetrics(dm);
		this.width = dm.widthPixels;
		this.height = dm.heightPixels;

		this.mScroller = new Scroller(this, new DecelerateInterpolator(2.0F));
	}

	public void setSLIDE_POINT(float sLIDE_POINT) {
		this.SLIDE_POINT = sLIDE_POINT;
	}

	public void setDURATION(int dURATION) {
		this.DURATION = dURATION;
	}

	class BGView extends RelativeLayout {
		private float disX = 0.0F;
		private float disY = 0.0F;
		private float downX = 0.0F;
		private float downY = 0.0F;

		public BGView(Context context, AttributeSet attrs, int defStyleAttr) {
			super(context, attrs, defStyleAttr);
			init();
		}

		public BGView(Context context, AttributeSet attrs) {
			super(context, attrs, 0);
			init();
		}

		public BGView(Context context) {
			super(context);
			init();
		}

		private void init() {
			postInvalidate();
			BaseSlideActivity.this.SLIDE_POINT = DensityUtil.dip2px(
					BaseSlideActivity.this.getBaseContext(),
					BaseSlideActivity.this.SLIDE_POINT);
		}

		public boolean onTouchEvent(MotionEvent event) {
			float x = event.getX();
			float y = event.getY();
			switch (event.getAction()) {
			case 0:
				this.downX = x;
				this.downY = y;
				break;
			case 2:
				this.disX = (x - this.downX);
				this.disY = (y - this.downY);
				move();
				break;
			case 1:
				BaseSlideActivity.this.MOVE_STATE = false;
				autoScroll();
				break;
			case 3:
			}

			return true;
		}

		public boolean onInterceptTouchEvent(MotionEvent event) {
			float x = event.getX();
			float y = event.getY();
			switch (event.getAction()) {
			case 0:
				this.downX = x;
				this.downY = y;
				break;
			case 2:
				this.disX = (x - this.downX);
				this.disY = (y - this.downY);
				move();
				break;
			case 1:
				BaseSlideActivity.this.MOVE_STATE = false;
				autoScroll();
				break;
			case 3:
			}

			return false;
		}

		private void move() {
			BaseSlideActivity.this.MOVE_STATE = true;
			if (BaseSlideActivity.this.mScroller.isFinished()) {
				BaseSlideActivity.this.rootView.layout((int) this.disX, 0,
						(int) (this.disX + BaseSlideActivity.this.width),
						BaseSlideActivity.this.height);
			}

			float b = 1.0F - Math.abs(this.disX / BaseSlideActivity.this.width);
			BaseSlideActivity.this.downView.setAlpha(b);
		}

		public void computeScroll() {
			if (BaseSlideActivity.this.mScroller.isFinished()) {
				return;
			}
			if ((!BaseSlideActivity.this.mScroller.computeScrollOffset())
					|| (BaseSlideActivity.this.mScroller.isFinished()))
				return;
			float cx = BaseSlideActivity.this.mScroller.getCurrX();
			float cy = BaseSlideActivity.this.mScroller.getCurrY();
			float fx = BaseSlideActivity.this.mScroller.getFinalX();
			float fy = BaseSlideActivity.this.mScroller.getFinalY();

			switch (BaseSlideActivity.this.UP_STATE) {
			case 0:
				if (!BaseSlideActivity.this.MOVE_STATE) {
					BaseSlideActivity.this.rootView
							.layout((int) (this.disX - cx), 0, (int) (this.disX
									- cx + BaseSlideActivity.this.width),
									BaseSlideActivity.this.height);
					float b = 1.0F - Math.abs((this.disX - cx)
							/ BaseSlideActivity.this.width);
					BaseSlideActivity.this.downView.setAlpha(b);
				} else {
					return;
				}
			case 1:
				if (!BaseSlideActivity.this.MOVE_STATE) {
					BaseSlideActivity.this.rootView
							.layout((int) (this.disX + cx), 0, (int) (this.disX
									+ cx + BaseSlideActivity.this.width),
									BaseSlideActivity.this.height);
					float b = 1.0F - Math.abs((this.disX + cx)
							/ BaseSlideActivity.this.width);
					BaseSlideActivity.this.downView.setAlpha(b);
					if (BaseSlideActivity.this.mScroller.getCurrX() == BaseSlideActivity.this.mScroller
							.getFinalX()) {
						BaseSlideActivity.this.downView.setVisibility(8);
						BaseSlideActivity.this.finish();
						return;
					}
				} else {
					return;
				}
			}

			postInvalidate();
		}

		private void autoScroll() {
			if ((this.disX <= BaseSlideActivity.this.SLIDE_POINT)
					&& (this.disX >= 0.0F)) {
				BaseSlideActivity.this.UP_STATE = 0;
				BaseSlideActivity.this.mScroller.startScroll(0, 0,
						(int) this.disX, 0, BaseSlideActivity.this.DURATION);
				postInvalidate();
			} else if ((this.disX > BaseSlideActivity.this.SLIDE_POINT)
					&& (this.disX <= BaseSlideActivity.this.width)) {
				BaseSlideActivity.this.UP_STATE = 1;
				BaseSlideActivity.this.mScroller.startScroll(0, 0,
						(int) (BaseSlideActivity.this.width - this.disX), 0,
						BaseSlideActivity.this.DURATION);
				postInvalidate();
			} else if ((this.disX > -BaseSlideActivity.this.SLIDE_POINT)
					&& (this.disX < 0.0F)) {
				BaseSlideActivity.this.UP_STATE = 0;
				BaseSlideActivity.this.mScroller.startScroll(0, 0,
						(int) this.disX, 0, BaseSlideActivity.this.DURATION);
				postInvalidate();
			} else if ((this.disX < -BaseSlideActivity.this.SLIDE_POINT)
					&& (this.disX > -BaseSlideActivity.this.width)) {
				BaseSlideActivity.this.UP_STATE = 1;
				BaseSlideActivity.this.mScroller.startScroll(0, 0,
						-(int) (BaseSlideActivity.this.width + this.disX), 0,
						BaseSlideActivity.this.DURATION);
				postInvalidate();
			}
		}
	}
}
